package com.sskj.spk.presenter;

import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.bean.BibiCoinType;
import com.sskj.spk.bean.AppVersionBean;
import com.sskj.spk.bean.SocketBean;
import com.sskj.spk.bean.SocketSendBean;
import com.sskj.spk.http.HttpConfig;
import com.sskj.spk.ui.activity.MainActivity;
import com.sskj.lib.Constans;
import com.sskj.lib.bean.StockCoinBean;
import com.sskj.lib.bean.WSFiveBean;
import com.sskj.lib.http.JsonCallBack;

import org.reactivestreams.Publisher;

import java.util.ArrayList;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;


public class MainActivityPresenter extends BasePresenter<MainActivity> {
    private WebSocketObserver stockSocket;
    private Disposable stockDisposable;
    private WebSocketObserver stockSocketFive;
    private Disposable stockDisposableFive;

    public void initWebSocket() {
        if (stockSocket == null) {
            RxBus.getDefault().register(this);
            stockSocket = httpService.pushCoinType(Constans.NO_FIVE_PUSH);
            stockDisposable = stockSocket
                    .toFlowable(BackpressureStrategy.DROP)
                    .flatMap((Function<String, Publisher<String>>) text -> {
                        ArrayList<String> strings = new ArrayList<>();
                        strings.add(text.substring(0, text.indexOf("}") + 1));
                        return Flowable.fromIterable(strings);
                    })
                    .map(s -> new Gson().fromJson(s, StockCoinBean.class))
                    .subscribe(data -> RxBus.getDefault().post(data));
            stockSocketFive = httpService.pushCoinFive(Constans.DEFAULT_BIBI_COIN);
            stockDisposableFive = stockSocketFive
                    .toFlowable(BackpressureStrategy.DROP)
                    .map(s -> new Gson().fromJson(s, WSFiveBean.class))
                    .subscribe(data -> RxBus.getDefault().post(data));
        } else {
            stockSocket.send(new Gson().toJson(new SocketSendBean(Constans.NO_FIVE_PUSH)));
            stockSocketFive.send(new Gson().toJson(new SocketSendBean(Constans.DEFAULT_BIBI_COIN)));
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeFive(BibiCoinType bibiCoinType) {
        if (stockSocketFive != null) {
            stockSocketFive.send(new Gson().toJson(new SocketSendBean(bibiCoinType.getCode())));
        }
    }

    // 获取长连接地址
    public void getSocket() {
        httpService.getSocket()
                .execute(new JsonCallBack<HttpData<SocketBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<SocketBean>> response) {
                        HttpData<SocketBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            HttpConfig.WS_STOCK = httpData.getData().getTicket();
                            HttpConfig.WS_STOCK_PANKOU = httpData.getData().getDepth();
                            initWebSocket();
                        }
                    }
                });
    }

    public void closeWebSocket() {
        stockSocket = null;
        stockSocketFive = null;
        DisposUtil.close(stockDisposable);
        DisposUtil.close(stockDisposableFive);
    }

    public void getNewVersion() {
        httpService.getVersion()
                .tag(this)
                .execute(new JsonCallBack<HttpData<AppVersionBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<AppVersionBean>> response) {
                        HttpData<AppVersionBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.onVersionDataSuccess(httpData.getData());
                        }
                    }
                });
    }


}
